package com.voute.Presenter.Impl;

import android.content.Context;

import com.voute.Consts.Constants;
import com.voute.Model.Bean.BaseBean;
import com.voute.Model.Bean.RegistResponseBean;
import com.voute.Model.Bean.UxTagBean;
import com.voute.Model.IRegisterModel;
import com.voute.Model.IUxTagModel;
import com.voute.Model.Impl.RegisterModelImpl;
import com.voute.Model.Impl.UXTagModelImpl;
import com.voute.Presenter.IRegisterCountPresenter;
import com.voute.Utils.CommonSubscriber;
import com.voute.Utils.RSAUtils;
import com.voute.Utils.TransformUtils;
import com.voute.View.IView.IRegisterView;

import java.util.HashMap;

import rx.Observable;
import rx.functions.Func1;

/**
 * 注册count的presenter接口实现类
 * Created by liucxu on 2017/1/16.
 */

public class RegisterCountPresenterImpl implements IRegisterCountPresenter {
    private IRegisterView registerView;
    private IRegisterModel model;
    private IUxTagModel tagModel;
    private Context context;

    public RegisterCountPresenterImpl(IRegisterView view, Context c) {
        if (c != null) {
            context = c;
            model = new RegisterModelImpl();
            tagModel = new UXTagModelImpl();
            registerView=view;
        }
    }

    public void getRegisterCode(final String phone) {
        registerView.showLoading();
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<BaseBean>>() {
            @Override
            public Observable<BaseBean> call(UxTagBean uxTagBean) {
                HashMap<String, String> values = new HashMap<String, String>();
                values.put("phone", phone);
                values.put("stype", "reg");
                values.put("uxtag", uxTagBean.getData().getUxtag());
                return model.getSMSCode(values);
            }
        }).compose(TransformUtils.<BaseBean>defaultSchedulers()).subscribe(new CommonSubscriber<BaseBean>() {
            @Override
            public void onSuccess(BaseBean response) {
                registerView.hideLoading();
                registerView.setRightVeritificationCode();
            }

            @Override
            public void onFailure(BaseBean response) {
                registerView.hideLoading();
                registerView.setErrorVeritificationCode(response);
            }
        });
    }

    public void registerCount(final String phone, final String code, final String username, final String password) {
        registerView.showLoading();
        tagModel.getUxTag().flatMap(new Func1<UxTagBean, Observable<RegistResponseBean>>() {
            @Override
            public Observable<RegistResponseBean> call(UxTagBean uxTagBean) {
                HashMap<String, String> values = new HashMap<String, String>();
                values.put("phone", phone);
                values.put("sms", code);
                values.put("username", username);
                try {
                    String encrypiedPassword=RSAUtils.encrypyByPublicKey(password.getBytes(), Constants.getRsaKey());
                    values.put("password", encrypiedPassword);
                } catch (Exception e) {
                    e.printStackTrace();
                }
                values.put("uxtag", uxTagBean.getData().getUxtag());
                return model.registerCount(values);
            }
        }).compose(TransformUtils.<RegistResponseBean>defaultSchedulers()).subscribe(new CommonSubscriber<RegistResponseBean>() {
            @Override
            public void onSuccess(RegistResponseBean response) {
                registerView.hideLoading();
                registerView.doRegisterCountSuccess(response);
            }

            @Override
            public void onFailure(BaseBean response) {
                registerView.hideLoading();
                registerView.doRegisterFailure(response);
            }
        });
    }
}
